from collections import defaultdict


def get_longest_path(n, edges):
    adjs = defaultdict(list)
    inv_adjs = defaultdict(list)
    for u, v in edges:
        adjs[u].append(v)
        inv_adjs[v].append(u)
    sources = set(range(n)).difference(set(inv_adjs.keys()))

    def dfs():
        white, gray, black = 0, 1, 2
        colors = [white] * n
        topo_path = []
        def visit(u):
            colors[u] = gray
            for v in adjs[u]:
                # no need for cycle check
                if colors[v] == white:
                    visit(v)
            colors[u] = black
            topo_path.append(u)
        for s in sources:
            if colors[s] == white:
                visit(s)
        topo_path.reverse()
        return topo_path
    topo_path = dfs()
    costs = [0] * n
    for v in topo_path:
        if inv_adjs[v]:
            costs[v] = max(costs[u] for u in inv_adjs[v]) + 1
        else:
            costs[v] = 1
    return max(costs)


if __name__ == "__main__":
    n = int(input().strip())
    n_edges = int(input().strip())
    edges = [input() for _ in range(n_edges)]
    edges = [line.strip().split('>') for line in edges]
    edges = [[int(x[1]) - 1, int(x[0]) -1] for x in edges]
    result = get_longest_path(n, edges)
    print(result)

